
#include "GraphicUtils.h"


GraphicsUtil* GraphicsUtil::mInstance;

GraphicsUtil::GraphicsUtil()
{
	//init font 
	SDL_Color color = {255, 132, 0};
	TTF_Font* font = TTF_OpenFont("framd.ttf", 24);

	char buf[3];
	for(int i=0;i<100;i++)  //100 = max number allowed to draw
	{
		itoa(i,buf,10);
		surface[i]=TTF_RenderText_Blended(font,buf , color);
	}
}

GraphicsUtil::~GraphicsUtil()
{
	//
}

SDL_Surface* GraphicsUtil::Create_SDLSurface(const char* ImgFile,int R, int G, int B)
{
	SDL_Surface* temp(0);
	SDL_Surface* result(0);
	if((temp= IMG_Load(ImgFile)))
	{
		if(temp->format->Amask)
		{
			result= SDL_DisplayFormatAlpha(temp);
		}
		else
		{
			result= SDL_DisplayFormat(temp);
		}
		SDL_FreeSurface(temp);
	}
	return result;
}

SDL_Surface* GraphicsUtil::Create_SDLSurface(const char* ImgFile,bool isAddToContainer)
{
	SDL_Surface* temp=Create_SDLSurface(ImgFile,1,1,1);
	if(isAddToContainer)
	{
		mSDL_Surface_Container.push_back(temp);
	}
	return temp;
}

void GraphicsUtil::ClearSDLSurfaces()
{
	std::vector<SDL_Surface*>::iterator it;
	for(it=mSDL_Surface_Container.begin();it!=mSDL_Surface_Container.end();it++)
	{
		SDL_FreeSurface(*it);
	}
	mSDL_Surface_Container.clear();
}

bool GraphicsUtil::Render_SDLSurface(SDL_Surface* dest, SDL_Surface* src, int x, int y)
{
	SDL_Rect DestR;
    DestR.x = x;
    DestR.y = y;
    SDL_BlitSurface(src, NULL, dest, &DestR);
	return true;
}

void GraphicsUtil::DrawNumber(SDL_Surface* screen,unsigned int number,Sint16 x, Sint16 y)
{
	SDL_Rect rect = {x, y};
	rect.x -= surface[number]->w / 2;
	rect.y -= surface[number]->h / 2;
	SDL_BlitSurface(surface[number], NULL, screen, &rect);
}

void GraphicsUtil::DrawText(SDL_Surface* screen, string text, int size, Sint16 x, Sint16 y, Uint8 red, Uint8 green, Uint8 blue, bool center)
{
	SDL_Rect rect = {x, y};
	if(center)
	{
		rect.x -= surface[0]->w / 2;
		rect.y -= surface[0]->h / 2;
	}
	SDL_BlitSurface(surface[0], NULL, screen, &rect);
}

GraphicsUtil& GraphicsUtil::getInstance()
{
	if(mInstance==NULL)
	{
		mInstance= new GraphicsUtil();
	}
	return *mInstance;
}

/*
bool GraphicsUtil::Render_SDLSurface(SDL_Surface* dest, SDL_Surface* src, int x, int y, int x2, int y2, int w, int h)
{
	SDL_Rect DestR;
    DestR.x = x;
    DestR.y = y;
    SDL_Rect SrcR;
    SrcR.x = x2;
    SrcR.y = y2;
    SrcR.w = w;
    SrcR.h = h;
    SDL_BlitSurface(src, &SrcR, dest, &DestR);
    return true;
}

*/
/*bool GraphicsUtil::Render_SDLSurface((SDL_Surface* img,int srcX, int srcY,int dstX, int dstY,int width, int height)
{
   SDL_Rect srcRect;
   srcRect.x = srcX;
   srcRect.y = srcY;
   srcRect.w = width;
   srcRect.h = height;

   SDL_Rect dstRect;
   dstRect.x = dstX;
   dstRect.y = dstY;
   dstRect.w = width;
   dstRect.h = height;

   SDL_BlitSurface(img, &srcRect, m_screen, &dstRect);

   return true;
}*/